package com.example.news;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
public class MyAdapter extends BaseAdapter {

    private ArrayList<NewsData> newsData;

    private Context context;
    private LayoutInflater layoutInflater;

    public MyAdapter(Context context, ArrayList<NewsData> newsData) {
        this.context = context;
        this.newsData= newsData;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return newsData.size();
    }

    @Override
    public NewsData getItem(int position) {
        return newsData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_listview, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((NewsData) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(final NewsData dataBean, ViewHolder holder) {
        holder.tvContent.setText(dataBean.getTitle());
        setBitmap(holder.img1, dataBean.getThumbnail_pic_s());
        setBitmap(holder.img2, dataBean.getThumbnail_pic_s02());
        setBitmap(holder.img3, dataBean.getThumbnail_pic_s03());
        holder.tvTime.setText(dataBean.getDate());
        holder.imgDelete.setImageResource(R.mipmap.icon_delete);
        holder.imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newsData.remove(dataBean);
                MyAdapter.this.notifyDataSetChanged();
            }
        });
    }

    private void setBitmap(ImageView img, String top) {
        if (top == null) {
            img.setVisibility(View.INVISIBLE);
        } else {
            img.setVisibility(View.VISIBLE);
            Picasso.with(context).load(top).into(img);
        }
    }

    protected class ViewHolder {
        private TextView tvContent;
        private ImageView img1;
        private ImageView img2;
        private ImageView img3;
        private TextView tvTime;
        private TextView tvFrom;
        private ImageView imgDelete;

        public ViewHolder(View view) {
            tvContent = (TextView) view.findViewById(R.id.tv_content);
            img1 = (ImageView) view.findViewById(R.id.img_1);
            img2 = (ImageView) view.findViewById(R.id.img_2);
            img3 = (ImageView) view.findViewById(R.id.img_3);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.width = getWidth() / 3;
            img1.setLayoutParams(params);
            img2.setLayoutParams(params);
            img3.setLayoutParams(params);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            tvFrom = (TextView) view.findViewById(R.id.tv_from);
            imgDelete = (ImageView) view.findViewById(R.id.img_delete);
        }

        private int getWidth() {
            WindowManager windowManager = ((Activity) context).getWindowManager();
            Point point = new Point();
            windowManager.getDefaultDisplay().getSize(point);
            return point.x;
        }
    }
}